package com.wx.mall.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.wx.mall.sys.entity.OperateLog;
import com.wx.mall.sys.mapper.OperateLogMapper;
import com.wx.mall.sys.service.IOperateLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author j
 * @since 2020-04-27
 */
@Service
public class OperateLogServiceImpl extends ServiceImpl<OperateLogMapper, OperateLog> implements IOperateLogService {

    @Override
    public List<OperateLog> getList(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);

        List<OperateLog> list=this.list();
        return list;
    };

    @Override
    public List<OperateLog> getListBySelect(Integer pageNum, Integer pageSize, String operateName, Date operateTime) {
        PageHelper.startPage(pageNum,pageSize);
QueryWrapper<OperateLog> wrapper= new QueryWrapper<>();

if (operateName!=null&&!operateName.equals("")){
    wrapper.eq("operate_name",operateName);

};

        if (operateTime!=null){
            wrapper.ge("operate_time",operateTime);

        };



        List<OperateLog> list=this.list(wrapper);
        return list;
    };


    @Override
    public int deleteList(List<Long> ids) {

        boolean sucess=this.removeByIds(ids);

//1成功0失败
        return sucess?1:0;
    };


    @Override
    public int deleteListByType(int type) {

        Calendar calendar=Calendar.getInstance();
        calendar.setTime(new Date());

        List<OperateLog> list=this.list();
        boolean sucess=false;
//        一周之前
if(type==1){
calendar.add(calendar.DAY_OF_MONTH,-7);
Date date=calendar.getTime();

    for (OperateLog operateLog:list
         ) {
     boolean commit=false;
        if ( operateLog.getOperateTime()!=null){
            commit=operateLog.getOperateTime().before(date);

        };

        if(commit==true){
          sucess=this.removeById(operateLog.getId());
        }

    }

};
//        一个月之前
        if(type==2){
            calendar.add(calendar.MONTH,-1);
            Date date=calendar.getTime();

            for (OperateLog operateLog:list
            ) {
                boolean commit=false;
                if ( operateLog.getOperateTime()!=null){
                    commit=operateLog.getOperateTime().before(date);
                };

                if(commit==true){
                    sucess=this.removeById(operateLog.getId());
                }

            }
        };
//        三个月之前
        if(type==3){
            calendar.add(calendar.MONTH,-3);
            Date date=calendar.getTime();

            for (OperateLog operateLog:list
            ) {
                boolean commit=false;
                if ( operateLog.getOperateTime()!=null){
                    commit=operateLog.getOperateTime().before(date);
                };

                if(commit==true){
                    sucess=this.removeById(operateLog.getId());
                }

            }
        };
//        半年之前
        if(type==4){
            calendar.add(calendar.MONTH,-6);
            Date date=calendar.getTime();

            for (OperateLog operateLog:list
            ) {
                boolean commit=false;
                if ( operateLog.getOperateTime()!=null){
                    commit=operateLog.getOperateTime().before(date);
                };

                if(commit==true){
                    sucess=this.removeById(operateLog.getId());
                }

            }
        };
//        一年之前
        if(type==5){

            calendar.add(calendar.YEAR,-1);
            Date date=calendar.getTime();

            for (OperateLog operateLog:list
            ) {
                boolean commit=false;
                if ( operateLog.getOperateTime()!=null){
                    commit=operateLog.getOperateTime().before(date);
                };

                if(commit==true){
                    sucess=this.removeById(operateLog.getId());
                }

            }
        };



        return sucess?1:0;
    }
}
